<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;
class ContentController extends CommonController{
    //主页
    public function index(){
        $conds=array();
        $title=$_GET['title'];
        if($title){
            $conds['title']=$title;
        }
        if($_GET['catid']){
           $conds['catid']=intval($_GET['catid']);
        }
        $page=$_REQUEST['p'] ? $_REQUEST['p'] :1;
        $pageSize=$_REQUEST['pageSize'] ? $_REQUEST['pageSize']:5;
        $news=D("News")->getNews($conds,$page,$pageSize);
        $count=D("News")->getNewsCount($conds);
        $res= new \Think\Page($count,$pageSize);
        $pageres= $res->show();
        $positions=D("Position")->getNormalPositions();
        $this->assign("positions",$positions);
        $webSiteMenu=D("Menu")->getBarMenus();
        $this->assign("news",$news);
        $this->assign("pageres",$pageres);
        $this->assign('webSiteMenu',$webSiteMenu);
       
        $this->display();
    }
    public function add(){
       if(IS_POST){
        //form数据传过来做点判断      
           if(!isset($_POST['title'])||!$_POST['title']){
                 return show(0,"标题不存在");
           }       
           if(!isset($_POST['small_title'])||!$_POST['small_title']){
                 return show(0,"短标题不存在");
           } 
           if(!isset($_POST['catid'])||!$_POST['catid']){
                 return show(0,"文章栏目不存在");
           }
           if(!isset($_POST['keywords'])||!$_POST['keywords']){
                 return show(0,"关键字不存在");
           }
            if(!isset($_POST['content'])||!$_POST['content']){
                 return show(0,"内容不存在");
           }
            if($_POST['news_id']){
                //保存数据
                 return $this->save($_POST);
             }
             $newsId=D("News")->insert($_POST);
           if($newsId){
               $newsContentData['content']=$_POST['content'];
               $newsContentData['news_id']=$newsId;
               $cId=D("NewsContent")->insert($newsContentData);
               if($cId){
                   return show(1,"新增成功");
               }else{
                    return show(1,"主表新增成功，副表插入失败");
               }
           }else{
               
                return show(0,"新增失败"); 
           }
        }else{
        $webSiteBar=D("Menu")->getBarMenus();
        $titleFontColor= C("TITLE_FONT_COLOR");
        $copyFrom= C("COPY_FROM");
        $this->assign("webSiteBar",$webSiteBar);
        $this->assign("titleFontColor",$titleFontColor); 
        $this->assign("copyFrom",$copyFrom);
        $this->display();
      
        }
    }
    public function edit(){
      $newsId=$_GET['id'];
      if(!$newsId){
          //若不是，就跳转到文章首页
          $this->redirect("/admin.php?c=content");
      }
        
      $news=D("News")->find($newsId); 
      if(!$news){
             $this->redirect("/admin.php?c=content");
      }
       $newsContent=D("NewsContent")->find($newsId);
      if($newsContent){
          $news['content']=$newsContent['content'];
      }
        $webSiteBar=D("Menu")->getBarMenus();
        $titleFontColor= C("TITLE_FONT_COLOR");
        $copyFrom= C("COPY_FROM");
        $this->assign("webSiteBar",$webSiteBar);
        $this->assign("titleFontColor",$titleFontColor); 
        $this->assign("copyfrom",$copyFrom);
        $this->assign("news",$news);
     $this->display();    
        
    }
    
    public function save($data){
      $newId=$data['news_id']; 
       unset($data['news_id']);
    try{ 
        $id=D("News")->updateById($newId,$data);
        $newsContentData['content']=$data['content'];
         $condId=D("NewsContent")->updateNewsById($newId,$newsContentData);
        if($id===false ||$condId===false){
             return show(0,"更新失败");
        } 
         return show(1,"更新成功");  
      }catch(Exception $e){
          return show(0,$e->getMessage());
      }
       
    }
    
 public function setStatus() {
     $data=array(
       'id'=>intval($_POST['id']),  
       'status'=>intval($_POST['status']),  
     );
   return  parent::setStatus($data,'News');
 }   
 
//排序
public function listorder(){
 //获取JS传过来的ajax的数据
return parent::listorder("News");
}
//推送功能的开发
public function push(){
 $jumpUrl=$_SERVER["HTTP_REFERER"];
  $positionId=intval($_POST['position_id']);  
  $newsId=$_POST['push']; 
  if(!$newsId ||!is_array($newsId)){
      
    return show(0,"请选择文章ID进行推荐");  
  }
  
  if(!$positionId){
     return show(0,"没有选择推荐位"); 
   }
  try{   
   $news=D("News")->getNewsByNewsIdIn($newsId);
   if(!$news){
      return show(0,"没有相关的内容");
   }
   foreach($news as $new){
       $data=array(
        'position_id'=>$positionId,
        'title'=>$new['title'],
        'thumb'=>$new['thumb'],
        'news_id'=>$new['news_id'],
        'status'=>1,
        'create_time'=>$new['create_time'], 
      );
      $position=D("PositionContent")->insert($data);
      
   }
  }catch(Exception $e){
      return show(0,$e->getMessage());
  }
  return show(1,"推送成功",array('jump_url'=>$jumpUrl));
    
}











}